import Vue from 'vue'
import Vuex from 'vuex'
import userStore from './userStore'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  },
  getters: {
    doubleCount(state) {
      return state.count * 2;
    },
    doubleCountN: state => (n) => state.count * n
  },
  mutations: {
    increment(state, n) {
      state.count += n;
    },
    decrement(state) {
      state.count -= 1;
    }
  },
  actions: {
    asyncDecrement(context) {
      // context 拥有与 store 实例相同的方法和属性
      setTimeout(() => {
        context.commit('decrement'); // 可以在异步之后，调用 mutations 
      }, 1000);
    }
  },
  modules: {
    userStore
  }
})
